package reflace;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * todo
 *
 * @author zhengshan
 * @Date 2023/5/6 16:50
 */
public class reflectTest {

    private Date startDate;

    private Date endDate;

    public static void main(String[] args) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        reflectTest reflectTest1 = new reflectTest();
        reflectTest1.setStartDate(simpleDateFormat.parse("2020-10-12 00:00:00"));
        reflectTest1.setEndDate(simpleDateFormat.parse("2023-12-31 00:00:00"));
        reflectTest reflectTest2 = new reflectTest();
        reflectTest2.setStartDate(simpleDateFormat.parse("2022-10-5 00:00:00"));
        reflectTest2.setEndDate(simpleDateFormat.parse("2099-12-27 00:00:00"));
        List<reflectTest> list = new ArrayList<>();
        list.add(reflectTest1);
        list.add(reflectTest2);
        for(int i = 0; i < list.size(); i++){
            Date startDate1 = list.get(i).getStartDate();
            Date endDate1 = list.get(i).getEndDate();
            for(int j = i+1; j < list.size(); j++){
                Date startDate2 = list.get(j).getStartDate();
                Date endDate2 = list.get(j).getEndDate();
                if(rangesOverlap(startDate1, endDate1, startDate2, endDate2)){
                    System.out.println("----------------");
                }
            }
        }
    }

    public static boolean rangesOverlap(Date startDate1, Date endDate1, Date startDate2, Date endDate2) {
        // Check if either range is empty
        if (startDate1 == null || endDate1 == null || startDate2 == null || endDate2 == null) {
            return false;
        }
        if(startDate1.equals(startDate2) && endDate1.equals(endDate2)){
            return false;
        }

        // Check if the ranges overlap
        return !endDate1.before(startDate2) && !endDate2.before(startDate1);
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }


    public static boolean rangesOverlap(String start1, String end1, String start2, String end2) {
        // Parse the strings into LocalDate objects
        LocalDate startDate1 = LocalDate.parse(start1, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate endDate1 = LocalDate.parse(end1, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate startDate2 = LocalDate.parse(start2, DateTimeFormatter.ISO_LOCAL_DATE);
        LocalDate endDate2 = LocalDate.parse(end2, DateTimeFormatter.ISO_LOCAL_DATE);

        // Check if either range is empty
        if (startDate1 == null || endDate1 == null || startDate2 == null || endDate2 == null) {
            return false;
        }

        // Check if the ranges overlap
        return !endDate1.isBefore(startDate2) && !endDate2.isBefore(startDate1);
    }
}
